package _05sql.homework1.impl;

import _05sql.homework1.MarketRegionDao;
import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MarketRegionDaoImpl implements MarketRegionDao {
    @Override
    public List<String> getInformationByType(byte type)throws Exception {
        Connection connection = null;
        DriverManager.registerDriver(new Driver());
        String url = "jdbc:mysql://localhost:3306/db?useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
        connection = DriverManager.getConnection(url, username, password);
        PreparedStatement preparedStatement= connection.prepareStatement("select name from market_region where type=?");
        preparedStatement.setByte(1,type);
        ResultSet resultSet= preparedStatement.executeQuery();
        List<String> list = new ArrayList<>();
        while (resultSet.next()) {
            list.add(resultSet.getString("name"));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return list;
    }
}
